package org.afun.csms_android;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import org.afun.csms_android.domain.Stu2Score;
import org.afun.csms_android.domain.javaBean.TS;
import org.afun.csms_android.utils.MyTableOperator;
import org.afun.csms_android.utils.TableOperator;

import java.io.Serializable;

public class RecordScoreActivity extends AppCompatActivity {
    private TS ts;
    private Context mContext;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record_score);

        Intent intent = getIntent();
        this.ts = (TS) intent.getSerializableExtra("ts");
//        System.out.println(ts);
        this.mContext=this;


        TextView stuIdText=findViewById(R.id.stuId);
        TextView stuNameText=findViewById(R.id.stuName);
        TextView genderText=findViewById(R.id.gender);
        TextView couIdText=findViewById(R.id.couId);
        TextView couNameText=findViewById(R.id.couName);
        EditText scoreEdit=findViewById(R.id.score);
        Button okBtn=findViewById(R.id.okBtn);
        Button cancelBtn=findViewById(R.id.cancelBtn);

        stuIdText.setText(String.valueOf(ts.getStuId()));
        stuNameText.setText(ts.getStuName());
        genderText.setText(ts.getGender());
        couIdText.setText(String.valueOf(ts.getCouId()));
        couNameText.setText(ts.getCouName());
        scoreEdit.setText(String.valueOf(ts.getScore()));



        okBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(mContext).setTitle("提示")
                        .setMessage("您确定要提交成绩吗？")
                        .setPositiveButton("是", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                MyTableOperator myTableOperator=new MyTableOperator(mContext);
                                TableOperator tableOperator = myTableOperator.getTableOperator();
                                int ssId = ts.getSsId();
                                int score = Integer.parseInt(scoreEdit.getText().toString());
                                Stu2Score stu2Score = new Stu2Score();
                                stu2Score.setSsid(ssId);
                                stu2Score.setScore(score);
                                boolean b = tableOperator.recordScore(stu2Score);
                                if(b){
                                    new androidx.appcompat.app.AlertDialog.Builder(mContext)
                                            .setTitle("提示信息")
                                            .setMessage("提交成功！")
                                            .setPositiveButton("确定",null)
                                            .create()
                                            .show();
                                }else{
                                    new androidx.appcompat.app.AlertDialog.Builder(mContext)
                                            .setTitle("提示信息")
                                            .setMessage("提交失败...")
                                            .setPositiveButton("确定",null)
                                            .create()
                                            .show();
                                }

                            }
                        })
                        .setNegativeButton("否",null)
                        .show();
            }
        });

        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

}